package com.rjwh.dingdong.client.mvp.presenter.impl;

import android.content.Context;

import com.aishua.lib.task.AsResult;
import com.aishua.lib.utils.AsStrUtils;
import com.rjwh.dingdong.client.bean.jsonbean.ResAddBabyBean;
import com.rjwh.dingdong.client.bean.localbean.AddBabyBean;
import com.rjwh.dingdong.client.bean.localbean.SysLoginBabylistBean;
import com.rjwh.dingdong.client.constant.NetConstant;
import com.rjwh.dingdong.client.mvp.BasePresenter;
import com.rjwh.dingdong.client.mvp.model.impl.AddBabyBoundCompleteModel;
import com.rjwh.dingdong.client.mvp.presenter.IAddBabyBoundCompletePresenter;
import com.rjwh.dingdong.client.mvp.view.AddBabyBoundCompleteView;
import com.rjwh.dingdong.client.util.ToastUtil;

/**
 * @author Kay
 * @ClassName AddBabyBoundCompletePresenter
 * @Description
 * @date 2016-7-21
 * @history 2016-7-21 author: description:
 */
public class AddBabyBoundCompletePresenter extends BasePresenter<AddBabyBoundCompleteView> implements IAddBabyBoundCompletePresenter {

    private AddBabyBoundCompleteModel model;

    public AddBabyBoundCompletePresenter(Context context, AddBabyBoundCompleteView mMvpView) {
        super(context, mMvpView);
        model = new AddBabyBoundCompleteModel(this);
    }

    @Override
    public void onServerDataAcquired(int businessType, AsResult mAsResult, boolean isRefresh) {
        if (isViewAttached()) {
            getMvpView().dismissLoading();
            switch (businessType) {
                case NetConstant.ADDBABY:
                    ResAddBabyBean addBabyBean = (ResAddBabyBean) mAsResult.getObj();
                    if (mAsResult.getResultCode() > NetConstant.MSG_CONNECT_SUCCESS && addBabyBean != null) {
                        AddBabyBean data = addBabyBean.getData();
                        if (data != null) {
                            SysLoginBabylistBean babyobj = data.getBabyobj();
                            if (babyobj != null) {
                                getMvpView().loadData(babyobj);
                            }
                        }
                    } else if (!AsStrUtils.isEmpty(mAsResult.getResultMsg())) {
                        ToastUtil.showToast(getContext(), mAsResult.getResultMsg());
                    }
                    break;

                default:
                    break;
            }
        }

    }

    @Override
    public void detachView() {
        detachViews();
        model.detachView();
    }

    /**
     * 添加宝宝
     *
     * @param childid
     * @param childname
     * @param birthday
     * @param sex
     * @param title     称谓
     */
    @Override
    public void addBaby(String childid, String childname, String birthday, String sex, String title) {
        if (isViewAttached()) {
            getMvpView().showLoading();
            model.addBaby(childid, childname, birthday, sex, title);
        }
    }

}
